//index.js
//获取应用实例
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;

Page({
  data: {
    longitude: 0,
    latitude: 0,
    markers: [],
    shopList: [], //店铺列表 
    navigatorHidden: true,
    show_authorization:false,  //授权判断
    cursor:false,
    valueText:'',
    valueCancel:false
  },
  onLoad: function () {
    qqmapsdk = new QQMapWX({
      key: '25FBZ-XOBW3-CHH3V-YBMKG-IVNWT-FYBRE'
    });
  },
  onShow:function(){

    // if (this.data.shop_select){
    //   this.setData({
    //     shopName: this.data.shop_select.shop_name,
    //     shopAddress: this.data.shop_select.address,
    //     distance: this.data.shop_select.distance,
    //     latitude: this.data.shop_select.add_info.location_latitude,
    //     longitude: this.data.shop_select.add_info.location_longitude,
    //     shopId: this.data.shop_select.id,
    //     navigatorHidden:false
    //   })
    // }

    this.adjustLocation();

    this.setData({
      img_url: app.globalData.base_url +'images/shopList_01.jpg'
    })
  },
  onHide(){
    this.setData({
      valueText:''
    })
  },
  //失去焦点
  bindblur:function(){
    this.setData({
      cursor:false
    })
  },
  //获取焦点
  bindfocus: function (e) {
    this.setData({
      cursor: true
    })
  },
  //输入搜索文字
  searchValue:function(e){
    var value = e.detail.value;
    if (value.length > 0){
      this.setData({
        valueCancel:true
      })
    }else{
      this.setData({
        valueCancel: false
      })
    }
  },
  //清空value
  emptyValue:function(){
    var that  = this;
      that.setData({
        valueCancel: false,
        valueText: ''
      })
  },
  //获取店铺列表
  getMarker: function () {
    var that = this;
    var latitude = this.data.latitude;
    var longitude = this.data.longitude;
    api.POST({
      params: { page: 1, limit: 100, longitude: longitude, latitude: latitude, current_longitude: longitude, current_latitude: latitude},
      url: app.globalData.base_url + 'shop/shopList',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          
          var shop = res.data.data.currentList;
          var shopId = that.data.shopId;
          if (shop.length > 0) {
            var markers = [], shopList = [];
            for (let i in shop) {
              if (shop[i].id == shopId) {
                var marker = {
                  id: i,
                  latitude: shop[i].add_info.location_latitude,
                  longitude: shop[i].add_info.location_longitude,
                  iconPath: '/images/ic_point_a.png',
                  width: 60,
                  height: 60,
                }
              } else {
                var marker = {
                  id: i,
                  latitude: shop[i].add_info.location_latitude,
                  longitude: shop[i].add_info.location_longitude,
                  iconPath: '/images/ic_point.png',
                  width: 50,
                  height: 50,
                }
              }
              markers.push(marker)
              var new_shop = shop[i];
              new_shop.shopImage = ((new_shop.shopImage == '' || new_shop.shopImage == undefined) ? '../../images/icon_59.png' : new_shop.shopImage);
              shopList.push(new_shop)
            }
            that.setData({
              shopList: shop,
              markers: markers
            })
          }
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("网络错误");
      }
    })
  },

  //定位
  adjustLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          show_authorization: false
        })
        that.getMarker();
      },
      fail: function (fail_res) {
        // console.log(fail_res)
         that.setData({
            show_authorization: true
         })
      //   if (fail_res.errMsg == "getLocation:fail auth deny" || fail_res.errMsg == "getLocation:fail:auth denied"){
      //     // that.showModal();
      //     that.setData({
      //       show_authorization:true
      //     })
      //   } else {
      //      that.setData({
      //         show_authorization: true
      //      })
      //   }
      }
    })
  },
  //点击标记点触发
  markerTap: function (e) {
    var that = this;
    var markerId = e.markerId;
    // console.log(e);
    var shop = that.data.shopList[markerId]

    // wx.setStorageSync('shop_select', shop)
    that.setData({
      shop_select: shop
    })
    var markers = that.data.markers;
    for (var i = 0; i < markers.length; i++){
      if (markers[i].id == markerId){
        markers[i].iconPath = '../../images/ic_point_a.png',
        markers[i].width= 60
        markers[i].height= 60
      }else{
       markers[i].iconPath = '../../images/ic_point.png',
       markers[i].width = 50
      markers[i].height = 50
      }
    }
    that.setData({
      markers:markers,
      navigatorHidden: false,
      shopName: shop.shop_name,
      shopAddress: shop.address,
      distance: shop.distance,
       business_hours: shop.business_hours,
      shopLat: shop.add_info.location_latitude,
      shopLon: shop.add_info.location_longitude,
      shopId: shop.id,
      topHeight:60,
    })
  },

  //获取授权信息
  getSetting: function () {
    var that = this;
      setTimeout(function(){
        that.adjustLocation();
      },300)
  },
  shopSearch:function(e){
    util.showLoading("加载中...");
    var value = e.detail.value
    var latitude = this.data.latitude;
    var longitude = this.data.longitude;
    // 调用接口
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        // console.log(res);
        if (res.status==0){
          var city = res.result.address_component.city;  //当前城市
          // console.log(city);

          qqmapsdk.getSuggestion({
            keyword: value,
            region: city,
            success: function (res) {
              util.hiddenLoading();
              var data = res.data;
              if (data.length > 0) {
                var lat = data[0].location.lat
                var lng = data[0].location.lng
                var searchCity = data[0].city   //搜索的目的地城市
                wx.navigateTo({
                  url: '/map/pages/shopSearch/shopSearch?lat=' + lat + '&lng=' + lng + '&searchCity=' + city + '&searchText=' + value
                })

              } else {
                util.hiddenLoading();
                util.showError("暂时没有找到相关信息");
              }
            },
            complete: function (res) {
              util.hiddenLoading();
              if (res.status != 0) {
                util.showError('暂时没有找到相关信息');
              }
            }
          });

        }
      },
      fail: function (res) {
        // console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });

  },
  // 导航
  navigation:function(){
    var latitude = this.data.shopLat/1;
    var longitude = this.data.shopLon/1;
    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    })
  },
  //查看店内商品
  shopProduct:function(){
    var shopId = this.data.shopId
    wx.navigateTo({
      url: '/map/pages/shopProduct/shopProduct?shopId=' + shopId+'&index=1'
    })
  }

})
